#include <stdio.h>
#include <file_opt.h>
#include <cJSON.h>
#include <list.h>
 
union value
{
    BOOL bl;
    int in;
    float f;
};
 
struct data
{
    int key;
    int type;
    union value val;
    struct list_head list;
};
int main(int argc, char const *argv[])
{
    char buf[1024] = {0};
    int ret = fileopt_readall("./test.json",buf);
    if (ret <0)
    {
        perror("read all error\n");
        return -1;
    }
    struct data sdata[3];
    struct data head;
    INIT_LIST_HEAD(&head.list);
 
    cJSON *json = NULL;
    cJSON *item_0 = NULL;
    cJSON *item = NULL;
    cJSON *item1 = NULL;
    cJSON *item2 = NULL;
    cJSON *item3 = NULL;
 
    json = cJSON_Parse(buf);
    if (!json)
    {
        printf("josn err.");
        return -1;
    }
    
    printf("获取ver下的值\n");
    char *ver = NULL;
    item_0 = cJSON_GetObjectItem(json, "ver");
    if(item_0 != NULL)
    {
        if(item_0->type == cJSON_String)
        {
            ver = item_0->valuestring;
            printf("ver = %s\n",ver);
        }
    }

    printf("获取cloud下的内容\n");
    char *password = NULL;
    char *mpassword = NULL;
    item = cJSON_GetObjectItem(json,"cloud");
    if(item != NULL)
    {
        cJSON *val = NULL;
        val = cJSON_GetObjectItem(item,"password");
        if(val != NULL && val->type == cJSON_String)
        {
            password = val->valuestring;
            printf("password = %s\n",password);
        }

        val = cJSON_GetObjectItem(item,"mpassword");
        if(val != NULL && val->type == cJSON_String)
        {
            mpassword = val->valuestring;
            printf("mpassword = %s\n",mpassword);
        }
    }


    item1 = cJSON_GetObjectItem(json, "data");
    int i;
    for (i = 0; i < 3; i++)
    {
        item2 = cJSON_GetArrayItem(item1, i);
        item3 = cJSON_GetObjectItem(item2, "key");
        sdata[i].key = item3->valueint;
 
        item3 = cJSON_GetObjectItem(item2, "type");
        sdata[i].type = item3->valueint;
 
        item3 = cJSON_GetObjectItem(item2, "val");
       switch (sdata[i].type)
        {
        case 1:
            sdata[i].val.bl = atoi(item3->valuestring);
            printf("key=%d,type=%d,val=%d\n",sdata[i].key,sdata[i].type,sdata[i].val.bl);
            break;
        case 2:
            sdata[i].val.in = atoi(item3->valuestring);
            printf("key=%d,type=%d,val=%d\n",sdata[i].key,sdata[i].type,sdata[i].val.in);
            break;
        case 3:
            sdata[i].val.f = atof(item3->valuestring);
            printf("key=%d,type=%d,val=%f\n",sdata[i].key,sdata[i].type,sdata[i].val.f);
            break;
        }
 
        list_add(&sdata[i].list, &head.list);
    }
    cJSON_Delete(json);

    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root,"ver",ver);
    cJSON *cloud = cJSON_CreateObject();
    cJSON_AddItemToObject(cloud,"password",cJSON_CreateString("12345678"));
    cJSON_AddItemToObject(cloud,"mpassword",cJSON_CreateString("12345678"));
    cJSON_AddItemToObject(root,"cloud",cloud);

    cJSON *data = NULL;
    cJSON_AddItemToObject(root,"data",data=cJSON_CreateArray());

    cJSON *data1 = cJSON_CreateObject();
    cJSON_AddItemToArray(data, data1);
    cJSON_AddItemToObject(data1,"key",cJSON_CreateNumber(1));
    cJSON_AddItemToObject(data1,"type",cJSON_CreateNumber(2));
    cJSON_AddItemToObject(data1,"val",cJSON_CreateString("10"));

    cJSON *data2 = cJSON_CreateObject();
    cJSON_AddItemToArray(data, data2);
    cJSON_AddItemToObject(data2,"key",cJSON_CreateNumber(2));
    cJSON_AddItemToObject(data2,"type",cJSON_CreateNumber(1));
    cJSON_AddItemToObject(data2,"val",cJSON_CreateString("0"));

    cJSON *data3 = cJSON_CreateObject();
    cJSON_AddItemToArray(data, data3);
    cJSON_AddItemToObject(data3,"key",cJSON_CreateNumber(3));
    cJSON_AddItemToObject(data3,"type",cJSON_CreateNumber(3));
    cJSON_AddItemToObject(data3,"val",cJSON_CreateString("22.5"));

    //FILE *fp = fopen("create.json","w+");
    char *printjson = cJSON_Print(root);
    file_opt_write("./create.json",printjson,strlen(printjson));
    //fwrite(buf,strlen(printjson),1,fp);
    //fclose(fp);
    cJSON_Delete(root);
    return 0;
}
